// pages/Searchflight/Searchflight.js

var api = require('../../api.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    f_v:"",
    list:null,
    showView: true,
    date: null,
    time: '12:01',
    trainBeginCity: '杭州',
    trainEndCity: '北京',
    countNumber:0,
    historylist: []
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  gotoResult: function (e) {
    
    var that = this;
    var flycode = e.detail.value.flycode;
    var flydate = e.detail.value.flydate;
    var flylou = e.detail.value.flylou;

    //var historyPlant = 
    //var historyPlantStr = wx.getStorageSync("historyPlant");
    var historylist = this.data.historylist;
    var is_ag = false;
    for (var i in historylist){
      if(historylist[i].code == flycode){
        is_ag = true;
      }
    }
    if (is_ag===false){
      historylist.push({ code: flycode, date: flydate });
      wx.setStorageSync("historyPlant", JSON.stringify(historylist));
    }
      
    wx.request({
      url: api.searchFlylist,
      data: { flyCode: flycode, flydate: flydate},
      success:function (res){
        that.setData({ countNumber: res.data.length });
        that.setData({list:res.data});
      },
      fail: function (){
        util.showTipError("接口返回失败");
      }
    })
  
  },
  checkFlyAction: function (e) {
    console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    console.log(e);
    wx.setStorage({
      key: "fight",
      data: {
        fight_id: e.currentTarget.dataset.id,
        departure:e.currentTarget.dataset.lou,
      }
    })
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    var fightinfo = e.currentTarget.dataset.code + " , " + e.currentTarget.dataset.time + " , " + e.currentTarget.dataset.lou
    var selectedFromPlant = { plantId: e.currentTarget.dataset.id, lat: e.currentTarget.dataset.lat, lng: e.currentTarget.dataset.lng };
    wx.setStorageSync("selectedFromPlant", JSON.stringify(selectedFromPlant));
    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。        
      fightinfo: fightinfo,
    });
    wx.navigateBack({
      delta: 1  // 返回上一级页面。
    })
  },
  history: function (e) {
    var key = e.target.dataset.historycode;    
    this.setData({ 
      f_v:key
    });
  },
  RemoveHistory: function () {
    this.setData({
      historylist: ""
    })
    wx.setStorageSync("historyPlant", []);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    showView: (options.showView == "true" ? true : false);
    console.log(util.formatDate(new Date()));
    this.setData({ 
      date: util.formatDate(new Date())
    });

    var historyPlantStr = wx.getStorageSync("historyPlant");
    
    if (historyPlantStr)
      this.setData({ historylist: JSON.parse(historyPlantStr) });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})